<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 5/24 0024
 * Time: 11:21
 */

namespace app\index\controller;

use think\Db;

use app\index\model\Manager;
use app\index\model\Shop;
use app\index\model\Wx;
use think\Loader;
use think\Session;
use think\View;
use think\Request;

class Card extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 会员卡列表
     * @return \think\response\View
     */
    public function mycard(){
        $merchantimodel=new Manager();
        $merchantid=$merchantimodel->getmerchantid(Session::get("managerid"));//获得商户ID
        View::share("res",model("Card")->getbymid($merchantid));
        return \view();
    }

    /**
     * 新建会员卡
     * @return \think\response\View
     */
    public function addcard(){
        if(request()->isAjax()){
            $merchantimodel=new Manager();
            $merchantid=$merchantimodel->getmerchantid(Session::get("managerid"));//获得商户ID
            $wx=new Wx();
            return json($wx->addcard($merchantid,request()->post()));
        }else{
            $shopmodel=new Shop();
            View::share("shoplist",$shopmodel->getallbyid(Session::get("managerid"),Session::get("usertype")));
            return \view();
        }
    }

    /**
     * 会员信息列表
     * @return \think\response\View
     */
    public function memberlist(){


        if(request()->param("starttime")){
            $starttime=strtotime(request()->param("starttime"));


        }
        if(request()->param('endtime')){
            $endtime=strtotime(request()->param('endtime'));


        }
        if(!empty($starttime)&&!empty($endtime)&&$starttime<$endtime){

            $wheresql=" and createtime>{$starttime} and createtime<{$endtime}";

        }elseif(!empty($starttime)){
            $wheresql=" and createtime>{$starttime}";


        }elseif (!empty($endtime)){
            $wheresql=" and createtime<{$endtime}";
        }

        if(request()->param("username")){
            $username=request()->param("username");
            $wheresql.=" and username like '%{$username}%' ";
        }


        $members=DB::query("select * from y_member where isdel=1 {$wheresql}");
        View::share('members',$members);

        return \view();
    }

    /**
     * 会员数据
     * @return \think\response\View
     */
    public function memberdata(){
    	echo'ddwew';
        return \view();
    }

    /**
     * 编辑会员
     * @return \think\response\View
     */
    public function memberedit(){
    	$id= request()->param('id');
    	$username=request()->param('username');
        $usertel=request()->param('usertel');
        $sex=request()->param('sex');
        $usergroup=request()->param('usergroup');
$method=request()->param('method');


            //echo json_encode(array('status'=>1,'msg'=>"更新成功"));
            if(Request::instance()->isAjax()){

            if(!empty($id)){

                            if($method==1){


                                            $rf1=DB::table('y_member')->where("id={$id}")->update(array('isdel'=>0));
                                            if($rf1){
                                                echo json_encode(array('status'=>1,'msg'=>"删除成功"));
                                                die;

                                            }else{
                                                echo json_encode(array('status'=>0,'msg'=>"删除失败"),2);

                                                die;

                                            }





                            }else{


                                            $rf1=DB::table('y_member')->where("id={$id}")->update(array('username'=>$username,'usertel'=>$usertel,'sex'=>$sex,"usergroup"=>$usergroup));
                                            if($rf1){
                                                echo json_encode(array('status'=>1,'msg'=>"更新成功"));
                                                die;

                                            }else{
                                                echo json_encode(array('status'=>0,'msg'=>"更新失败"),2);

                                                die;

                                            }





                            }





                            }else{

                                    $rf1=DB::table('y_member')->insert(array('username'=>$username,'usertel'=>$usertel,'sex'=>$sex,"usergroup"=>$usergroup));
                                    if($rf1){
                                        echo json_encode(array('status'=>1,'msg'=>"添加成功"));
                                        die;

                                    }else{
                                        echo json_encode(array('status'=>0,'msg'=>"添加失败".$username.'-1-'.$usertel."-2-".$sex,'usergroup'.$usergroup),2);

                                        die;

                                    }






                            }





        }



            if(!empty($id)){
                $member=DB::table('y_member')->where("id={$id} and isdel=1")->find();
                View::share('member',$member);


            }
        return \view();
    }

    /**
     * 会员运营
     * @return \think\response\View
     */
    public function memberopereate(){
        return \view();
    }

    /**
     * 优惠券列表
     * @return \think\response\View
     */
    public function ticketlist(){
        return \view();
    }

    /**
     * 编辑优惠券
     * @return \think\response\View
     */
    public function editticket(){
        return \view();
    }

    /**
     * 核销记录
     * @return \think\response\View
     */
    public function checkhistory(){
        return \view();
    }

    /**
     * 领券记录
     * @return \think\response\View
     */
    public function signticket(){
        return \view();
    }

    /**
     * 修改余额/积分
     * @return \think\response\View
     */
    public function scorehistory(){
        return \view();
    }
    
    /**
     * 修改余额/积分详情页
     */
    public function scoredetail(){
    	return \view();
    }

    /**
     * 导入会员
     */
    public function importmember(){
        return \view();
    }

    /**
     * 配置公众号
     */
    public function wxconfig(){
        $merchantimodel=new Manager();
        $merchantid=$merchantimodel->getmerchantid(Session::get("managerid"));
        $ori=model("Wxconfig")->where("merchantid",$merchantid)->find();
        if(request()->isPost()){
            $validate = Loader::validate('Wxconfig');
            if(!$validate->check(request()->post())){
                $this->error($validate->getError());
            }
            $data=[];
            $data["appid"]=request()->post("appid");
            $data["secret"]=request()->post("secret");
            $data["key"]=request()->post("key");
            $data["mchid"]=request()->post("mchid");
            $data["token"]=request()->post("token");
            //检查第一个文件
            $file=request()->file("sslcert_path");
            if ($file){
                $info=$file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'uploads');
                if($info){
                    $data['sslcert_path']=$info->getSaveName();
                }else{
                    $this->error($file->getError());
                    exit();
                }
            }
            //检查第二个文件
            $file1=request()->file("sslkey_path");
            if($file1){
                $info1=$file1->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'uploads');
                if($info1){
                    $data['sslkey_path']=$info1->getSaveName();
                }else{
                    $this->error($file1->getError());
                    exit();
                }
            }

            if($ori){
                $res=model("wxconfig")->isUpdate(true,["merchantid"=>$merchantid])->save($data);
            }else{
                $data["merchantid"]=$merchantid;
                $res=model("wxconfig")->save($data);//新增
            }
            if ($res){
                $this->success("保存成功");
            }else{
                $this->error("保存失败,请重试");
            }
        }else{
            if($ori){
                View::share("res",$ori);
            }
            return \view();
        }
    }

    /**
     * 上传图片到微信服务器
     */
    public function uploadfile(){
        if(!request()->file("ajaxfile")){
            return ["status"=>0];//文件不能为空
        }
        ###########################################
        $merchantimodel=new Manager();
        $merchantid=$merchantimodel->getmerchantid(Session::get("managerid"));//获得商户ID
        $wxmodel=new Wx();
        $access_token=$wxmodel->gettokenbyid($merchantid);
        ###########################################
        $res=$wxmodel->addpoi($access_token);
        $url="https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=$access_token";
        $data=[
            "buffer"=>new \CURLFile( request()->file("ajaxfile")->getRealPath())
        ];
        $uploadres=upload_file_55($url,$data);
        $uploadarr=json_decode($uploadres,true);
        if(isset($uploadarr["url"])){
            return $uploadarr["url"];
        }else{
            return $uploadarr["errmsg"];
        }
    }


    public function testcreate(){
        ###########################################
        $merchantimodel=new Manager();
        $merchantid=$merchantimodel->getmerchantid(Session::get("managerid"));//获得商户ID
        $wxmodel=new Wx();
        $access_token=$wxmodel->gettokenbyid($merchantid);
        ###########################################
//        dump($access_token);
//        $access_token="dsafwefq";
        $res=$wxmodel->createQrcode($access_token);
        return json(json_decode($res));
    }

    public function userlist(){
        ###########################################
        $merchantimodel=new Manager();
        $merchantid=$merchantimodel->getmerchantid(Session::get("managerid"));//获得商户ID
        $wxmodel=new Wx();
        $access_token=$wxmodel->gettokenbyid($merchantid);
        ###########################################
        $a=file_get_contents("https://api.weixin.qq.com/cgi-bin/user/get?access_token=$access_token&next_openid=");
        dump($a);
    }

    /**
     * 批量查询卡券列表
     */
    public function getcardlist(){
        ###########################################
        $merchantimodel=new Manager();
        $merchantid=$merchantimodel->getmerchantid(Session::get("managerid"));//获得商户ID
        $wxmodel=new Wx();
        $access_token=$wxmodel->gettokenbyid($merchantid);
        ###########################################
        $res=curl_post("https://api.weixin.qq.com/card/batchget?access_token=$access_token",json_encode([
            "offset"=>0,
            "count"=>50
        ]));
        dump($res);
    }

    public function testwhite(){
        ###########################################
        $merchantimodel=new Manager();
        $merchantid=$merchantimodel->getmerchantid(Session::get("managerid"));//获得商户ID
        $wxmodel=new Wx();
        $access_token=$wxmodel->gettokenbyid($merchantid);
        ###########################################
//        $access_token=$wxmodel->getAccessToken($appid,$secret);
//        dump($access_token);
        $url="https://api.weixin.qq.com/card/testwhitelist/set?access_token=$access_token";
        $poststr=json_encode([
            "username"=>["neisony"]
        ]);
        dump(curl_post($url,$poststr));die();
        $res=$wxmodel->testwhitelist($access_token,"neisony");
        dump($res);
    }

    //升级用户信息
    public function updateuser(){
        $merchantimodel=new Manager();
        $merchantid=$merchantimodel->getmerchantid(Session::get("managerid"));//获得商户ID
        $wxmodel=new Wx();
        $access_token=$wxmodel->gettokenbyid($merchantid);
        $res=$wxmodel->updateuser($access_token);
        dump($res);
    }

    //获得一个CODE
    public function testcode(){
        $wxmodel=new Wx();
        return $wxmodel->getacode();
    }

}